package com.rxjavademo.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.LogUtils;
import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import com.rxjavademo.R;
import com.rxjavademo.api.Api;
import com.rxjavademo.base.BaseActivity;
import com.rxjavademo.base.BasePresenter;
import com.rxjavademo.bean.HomeBean;
import com.rxjavademo.common.Constant;
import com.rxjavademo.presenter.MainActivityPresenter;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends BaseActivity<MainActivityPresenter>{


    private static final String TAG = "lzz";

    @Override
    protected int getLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
    }

    @Override
    public void showLoading() {
        super.showLoading();
    }




    /*
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        simpleUse();  //Rxjava1.0
//        ActionUse();  //Rxjava1.0
//        mapUser(); // 线程切换
//        FunAndMapUse();
        //https://www.jianshu.com/u/c50b715ccaeb
    }
*/

    @Override
    public MainActivityPresenter initPresenter() {
        return new MainActivityPresenter(this);
    }



    private void mapUser() {
        Observable.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
                emitter.onNext(1);
                emitter.onNext(2);
                emitter.onNext(3);
                Log.d(TAG, "subscribe: " + " onNext  thread:" + Thread.currentThread().getName());

            }
        }).map(new Function<Integer, String>() {
            @Override
            public String apply(Integer integer) throws Exception {
                return "No." + integer;
            }
        }).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.newThread()).subscribe(new Consumer<String>() {
            @Override
            public void accept(String s) throws Exception {
                Log.d(TAG, "accept: " + s + " thread:" + Thread.currentThread().getName());
            }
        });

    }

/*    private void FunAndMapUse() {
        Course chinese = new Course(1, "语文");
        Course english = new Course(2, "英语");
        Course math = new Course(3, "数学");
        Course polity = new Course(4, "政治");
        ArrayList<Course> courseList01 = new ArrayList<>();
        ArrayList<Course> courseList02 = new ArrayList<>();
        courseList01.add(chinese);
        courseList01.add(math);
        courseList02.add(english);
        courseList02.add(polity);
        Student student1 = new Student("student1", courseList01);
        Student student2 = new Student("student2", courseList02);
    }*/

    private void ActionUse() {
/*        Observable.just("123", "456", "789").subscribe(new Action1<String>() {
            @Override
            public void call(String s) {
                Log.d("lzz", "onNext :" + s);
            }
        }, new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {
                Log.d("lzz", "onError :");
            }
        }, new Action0() {
            @Override
            public void call() {
                Log.d("lzz", "onCompleted :");
            }
        });*/
    }

    private void simpleUse() {
        // 被观察者
 /*       Observable<String> observable = Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                subscriber.onNext("123");
                subscriber.onCompleted();
            }
        });

        // 观察者
        Observer<String> observer = new Observer<String>() {
            @Override
            public void onCompleted() {
                Log.d("lzz", "onCompleted :");
            }

            @Override
            public void onError(Throwable e) {
                Log.d("lzz", "onError :");
            }

            @Override
            public void onNext(String s) {
                Log.d("lzz", "onNext :" + s);
            }
        };


        // 订阅
        observable.subscribe(observer);*/
    }

    @OnClick(R.id.query_home)
    public void onViewClicked() {
//        presenter.queryHome();
        new Thread(new Runnable() {
            @Override
            public void run() {
                presenter.textQuery();
            }
        }).start();
    }
}
